﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Data;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.HomeServer.HomeServerConsoleTab.TV4Home.Server.WHS.AddIn.Controls
{
    public partial class SetupAndConfigurationUserControl : UserControl
    {
        public SetupAndConfigurationUserControl()
        {
            InitializeComponent();

            VerifyComponents();
        }

        private void VerifyComponents()
        {
            Utils.ComponentVerifier.VerificationResult tvServerResult = Utils.ComponentVerifier.CheckTVServer();
            Utils.ComponentVerifier.VerificationResult webInterfaceResult = Utils.ComponentVerifier.CheckTV4HomeWebInterface();
            Utils.ComponentVerifier.VerificationResult customConfigurationResult = Utils.ComponentVerifier.CheckCustomConfiguration();

            this.sacTVServer.State = tvServerResult.Status;
            this.sacWebInterface.State = webInterfaceResult.Status;
            this.sacCustomConfiguration.State = customConfigurationResult.Status;

            this.sacTVServer.MessageText = tvServerResult.VerificationMessage;
            this.sacWebInterface.MessageText = webInterfaceResult.VerificationMessage;
            this.sacCustomConfiguration.MessageText = customConfigurationResult.VerificationMessage;

            switch (tvServerResult.Status)
            {
                case Utils.ComponentVerifier.VerificationStatus.NotInstalled:
                    this.sacTVServer.CustomActionLinkText = "Install";
                    this.sacTVServer.CustomAction = new Action(InstallTVServer);
                    break;

                case Utils.ComponentVerifier.VerificationStatus.Installed:
                    this.sacTVServer.CustomActionLinkText = "Uninstall";
                    this.sacTVServer.CustomAction = new Action(UninstallTVServer);
                    break;
            }

            switch (webInterfaceResult.Status)
            {
                case Utils.ComponentVerifier.VerificationStatus.NotInstalled:
                    this.sacWebInterface.CustomActionLinkText = "Install";
                    this.sacWebInterface.CustomAction = new Action(InstallWebInterface);
                    break;

                case Utils.ComponentVerifier.VerificationStatus.Installed:
                    this.sacWebInterface.CustomActionLinkText = "Uninstall";
                    this.sacWebInterface.CustomAction = new Action(UninstallWebInterface);
                    break;
            }

            switch (customConfigurationResult.Status)
            {
                case Utils.ComponentVerifier.VerificationStatus.NotInstalled:
                    if (tvServerResult.Status != Utils.ComponentVerifier.VerificationStatus.NotInstalled)
                    {
                        this.sacCustomConfiguration.CustomActionLinkText = "Install";
                        this.sacCustomConfiguration.CustomAction = new Action(InstallCustomConfiguration);
                    }
                    else
                    {
                        this.sacCustomConfiguration.CustomActionLinkText = "";
                        this.sacCustomConfiguration.CustomAction = null;
                    }
                    break;
                case Utils.ComponentVerifier.VerificationStatus.Installed:
                    this.sacCustomConfiguration.CustomActionLinkText = "";
                    this.sacCustomConfiguration.CustomAction = null;
                    break;
            }
        }

        private void InstallTVServer()
        {
            try
            {
                new Wizards.InstallTVServer.InstallTVServerWizard().Run();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }

            VerifyComponents();
        }

        private void UninstallTVServer()
        {
            try
            {
                new Wizards.UninstallTVServer.UninstallTVServerWizard().Run();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }

            VerifyComponents();
        }

        private void InstallWebInterface()
        {
            if (System.IO.File.Exists(Utils.CommonPaths.WebInterface_MSIPath))
            {
                Process installProc = new Process();
                installProc.StartInfo = new ProcessStartInfo("msiexec.exe", String.Format("/i \"{0}\"", Utils.CommonPaths.WebInterface_MSIPath));

                installProc.Start();
                installProc.WaitForExit();
            }

            VerifyComponents();
        }

        private void UninstallWebInterface()
        {
            if (System.IO.File.Exists(Utils.CommonPaths.WebInterface_MSIPath))
            {
                Process uninstallProc = new Process();
                uninstallProc.StartInfo = new ProcessStartInfo("msiexec.exe", String.Format("/x \"{0}\"", Utils.CommonPaths.WebInterface_MSIPath));

                uninstallProc.Start();
                uninstallProc.WaitForExit();
            }

            VerifyComponents();
        }

        private void InstallCustomConfiguration()
        {
            // only run this method if TV Server is installed
            if (Utils.ComponentVerifier.CheckTVServer().Status == Utils.ComponentVerifier.VerificationStatus.NotInstalled)
                return;

            try
            {
                // rename SetupTv.exe.config to SetupTv.config
                System.IO.File.Move(Utils.CommonPaths.TVServer_InstallDirectory + @"\SetupTv.exe.config",
                                    Utils.CommonPaths.TVServer_InstallDirectory + @"\SetupTv.config");

                // rename TvService.exe.config to TvService.config
                System.IO.File.Move(Utils.CommonPaths.TVServer_InstallDirectory + @"\TvService.exe.config",
                                    Utils.CommonPaths.TVServer_InstallDirectory + @"\TvService.config");

                // restart TV service
                ServiceController service = new ServiceController("TvService");

                TimeSpan timeout = TimeSpan.FromSeconds(10);

                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running, timeout);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex);
            }

            VerifyComponents();
        }
    }
}
